package com.zw.pdm.module.est.controller;

import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.basic.Equ;
import com.zw.pdm.entity.basic.EquType;
import com.zw.pdm.entity.cost.CosProcess;
import com.zw.pdm.entity.cost.CosProcessDetail;
import com.zw.pdm.entity.craft.CraMakeDetail;
import com.zw.pdm.entity.designtask.DsnProStruct;
import com.zw.pdm.entity.est.Card;
import com.zw.pdm.entity.est.CardDetail;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.est.service.CreateCardService;
import com.zw.pdm.module.report.model.Project;
import com.zw.pdm.module.report.service.SheetService;
import com.zw.pdm.util.CommonUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

import static com.zw.pdm.constant.Constant.*;

/**
 * @Description: 创建工艺卡
 * @Author: China.sgl
 * @Date: 2020/8/3 16:11
 */
@Controller
@RequestMapping("/est/add")
@SuppressWarnings("all")
public class CreateCardController extends BaseController {

    @Autowired
    private CreateCardService createCardService;
    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private SheetService sheetService;


    @GetMapping
    @RequiresPermissions(value = {"est:add:view"}, logical = Logical.OR)
    public String page(Model model) {
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("routes", dictCommonService.findRoutes());
        model.addAttribute("processes", dictCommonService.findProcesses());
        model.addAttribute("equNames", dictCommonService.findEqu());
        model.addAttribute("verifys", dictCommonService.findPersonByCategory("工艺员"));
        return "est/add/page";
    }
    /** myf查询制作件清单列表*/
    @ResponseBody
    @PostMapping("/processList")
    public JsonResult processList(){
        return jsonResult(dictCommonService.findProcesses());

    }
    /**
     * 查询项目信息
     */
    @GetMapping("/project/list")
    @ResponseBody
    public List<Project> proList(String q) {
        return sheetService.findProjectByCode(q);
    }

    /**
     * 查询部分
     */
    @GetMapping("/part/list")
    @ResponseBody
    public List<DsnProStruct> partList(String code, String type) {
        return sheetService.findPartByProjectCode(code, type);
    }

    /**
     * 查询图号
     */
    @GetMapping("/drawNo/list")
    @ResponseBody
    public List<CraMakeDetail> drawNoList(String code, Integer partId) {
        return sheetService.findDrawNoByProCodeAndPart(code, partId);
    }

    /**
     * 查询图号
     */
    @GetMapping("/drawNo")
    @ResponseBody
    public CraMakeDetail drawNo(Integer id) {
        return sheetService.findDrawNoById(id);
    }

    /**
     * 保存工艺卡
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions(value = {"est:add:edit"}, logical = Logical.OR)
    public JsonResult save(Card card, String[] route, Integer[] orderNo, String[] process, String[] equName, String[] craftContent) {
        //验证开始 — — 徐一贺
        String warningMessage = "";
        if (orderNo.length < 2) {
            return error("工艺路线至少两个！");
        }
        //用来比较路线不同且不相邻但是序号相同的情况使用的临时序号Map
//        Map<Integer, String> orderNoList = new HashMap();
        for (int i = 0; i < orderNo.length; i++) {
//            if (orderNoList.get(orderNo[i]) == null) {
//                orderNoList.put(orderNo[i], route[i]);
//            } else if (i - 1 >= 0 && orderNo[i] != orderNo[i - 1] && orderNoList.get(orderNo[i]).equals(route[i])) {
//                return error("第" + (i + 1) + "个路线" + route[i] + "与之前不相邻的" + orderNoList.get(orderNo[i]) + "路线序号相同，不相邻的同名路线，序号必须不一致！");
//            } else if (i - 1 >= 0 && orderNo[i] != orderNo[i - 1] && orderNoList.get(orderNo[i])!=null && !orderNoList.get(orderNo[i]).equals(route[i])) {
//                return error("第" + (i + 1) + "个路线" + route[i] + "与之前不相邻的" + orderNoList.get(orderNo[i]) + "路线序号相同，不相邻的不同名路线，序号必须不一致！");
//            }
            if (i == 0) {
                if (orderNo[i] != 10) {
                    return error("首路线的序号必须是10！");
                }
                if (orderNo[i] == 10 && ("转储".equals(route[i]) || "参焊".equals(route[i]))) {
                    return error("路线名称可以是原料，也可以是其他，但不能是转储或者参焊");
                }
            }
            if (i == orderNo.length - 1) {
                if (!"转储".equals(route[i]) && !"参焊".equals(route[i])) {
                    return error("尾路线必须是转储或者参焊！");
                }
            }
//            if (i + 1 < orderNo.length && orderNo[i] > orderNo[i + 1]) {
//                return error("第" + (i + 2) + "道路线的序号必须大于前一个路线的序号！");
//            }
//            if (i + 1 < orderNo.length && route[i].equals(route[i + 1]) && orderNo[i] != orderNo[i + 1]) {
//                return error("第" + (i + 1) + "个路线" + route[i] + "与第" + (i + 2) + "道路线" + route[i + 1] + "的路线名相同，序号必须也要相同！");
//            }
//            if (i + 1 < orderNo.length && !route[i].equals(route[i + 1]) && orderNo[i] == orderNo[i + 1]) {
//                return error("第" + (i + 1) + "个路线" + route[i] + "与第" + (i + 2) + "道路线" + route[i + 1] + "的路线名不相同，序号必须也要不相同！");
//            }
        }
        //验证结束


        JsonResult jsonResult = jsonResult(createCardService.save(card, route, orderNo, process, equName, craftContent));
        if (jsonResult.getType() == JsonResult.ResultType.ERROR) {
            jsonResult.setMsg("图号已存在");
        }
        return jsonResult;
    }
    /** 根据工序查询设备*/
    @ResponseBody
    @PostMapping("/selectBuf")
    public JsonResult selectBuf(String name){
        List<Equ> equTypeList = createCardService.findBufByName(name);
        return jsonResult(equTypeList);

    }

}
